package com.hethongviet.job.helpers;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.util.Log;

public class ServiceHelper
{
	public static final String URL = "http://quanlytiendo.com/User/";

	public ServiceHelper()
	{
	}

	public static String doLogin(String username, String password,
			String activationCode)
	{
		String methodName = "CheckUserName";
		String sendData = "UserName=" + username + "&Password=" + password
				+ "&ActiveCode=" + activationCode;
		try
		{
			Log.d("Debug", URL + methodName + ".aspx?" + sendData);
			// InputStream inputStream = new OpenHttpConnection().get(URL +
			// methodName + ".aspx?"
			// + sendData);
			InputStream inputStream = new OpenHttpConnection().post(URL
					+ methodName + ".aspx", sendData);

			if (inputStream == null)
			{
				Log.d("Network error!", "Please check the network!");
			}
			else
			{
				Log.d("Debug", "connect ok!!!!!!!!");
				return getResultString(inputStream);
			}

		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	private static String getResultString(InputStream inputStream)
	{
		InputStreamReader inputStreamReader = new InputStreamReader(
				inputStream);
		/* Get the string return */
		int charRead = 0;
		String resultStr = "";
		char[] charBuffer = new char[2000];
		try
		{
			while ((charRead = inputStreamReader.read(charBuffer)) > 0)
			{
				resultStr += String.valueOf(charBuffer, 0, charRead);
				charBuffer = new char[2000];
			}
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.d("Debug", resultStr);
		return resultStr;
	}
	
	public static String doGetListOfProjects(String username, String memberId,
			boolean isManagerJobs)
	{
		String methodName = "GetJobList";
		String sendData = "FK_MemberID=" + memberId + "&UserName=" + username
				+ "&IsManagerJob=" + (isManagerJobs ? "True" : "False");
		try
		{
			Log.d("Debug", URL + methodName + ".aspx?" + sendData);
			InputStream inputStream = new OpenHttpConnection().post(URL
					+ methodName + ".aspx", sendData);

			if (inputStream == null)
			{
				Log.d("Network error!", "Please check the network!");
			}
			else
			{
				Log.d("Debug", "connect ok!!!!!!!!");
				return getResultString(inputStream);
			}

		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	public static String doGetListOfAllTasks(String username, String memberId,
			boolean isManagerJobs)
	{
		String methodName = "GetJobItemAll";
		String sendData = "FK_MemberID=" + memberId + "&UserName=" + username
				+ "&IsManagerJob=" + (isManagerJobs ? "True" : "False");
		try
		{
			Log.d("Debug", URL + methodName + ".aspx?" + sendData);
			InputStream inputStream = new OpenHttpConnection().post(URL
					+ methodName + ".aspx", sendData);

			if (inputStream == null)
			{
				Log.d("Network error!", "Please check the network!");
			}
			else
			{
				Log.d("Debug", "connect ok!!!!!!!!");
				return getResultString(inputStream);
			}

		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

}
